#include <iostream>
#include "filtre.hpp"
#include <openCV/highgui.h>
#include <openCV/cv.h>

const uchar* getPixel(const IplImage* img, int x,int y)
{
	return (uchar*)(img->imageData)+(y*img->widthStep)+x*img->nChannels;
}
uchar* getPixel(IplImage* img, int x,int y)
{
	return (uchar*)(img->imageData)+(y*img->widthStep)+x*img->nChannels;
}


IplImage* filtre(const IplImage* img,int l)
{
	int i,j,x,y;
	double b,r,v;
	double nbpixel=0;
	IplImage* tmp=cvCreateImage(cvSize(img->width,img->height),img->depth,3);
	IplImage* res=cvCreateImage(cvSize(img->width,img->height),img->depth,3);
	for(j=0; j<img->height;j++)
	{
		for(i=0;i<img->width;i++)
		{
			nbpixel=(2*l+1);
			b=0;
			r=0;
			v=0;
			for(y=j-l;y<=j+l;y++)
			{
					if(y<0 || y>=img->height)
					{
						nbpixel=nbpixel-1;
					}
					else
					{
						r+=getPixel(img,i,y)[0];
						v+=getPixel(img,i,y)[1];
						b+=getPixel(img,i,y)[2];
						
					}
			}
			getPixel(tmp,i,j)[0]=r/nbpixel;
			getPixel(tmp,i,j)[1]=v/nbpixel;
			getPixel(tmp,i,j)[2]=b/nbpixel;
			
		}
	}
	for(j=0; j<tmp->height;j++)
	{
		for(i=0;i<tmp->width;i++)
		{
			nbpixel=(2*l+1);
			b=0;
			r=0;
			v=0;
			for(x=i-l;x<=i+l;x++)
			{
					if(x<0 || x>=tmp->width)
					{
						nbpixel=nbpixel-1;
					}
					else
					{
						r+=getPixel(tmp,x,j)[0];
						v+=getPixel(tmp,x,j)[1];
						b+=getPixel(tmp,x,j)[2];
						
					}
			}
			getPixel(res,i,j)[0]=r/nbpixel;
			getPixel(res,i,j)[1]=v/nbpixel;
			getPixel(res,i,j)[2]=b/nbpixel;
			
		}
	}
	return res;
}


IplImage* filtreGaussien(const IplImage* img,double sigma)
{
	int i,j,x,y,m,k;
	//double b,r,v;
	double c[3];
	double coef;
	int l=sigma*3;
	IplImage* tmp=cvCreateImage(cvSize(img->width,img->height),img->depth,3);
	IplImage* res=cvCreateImage(cvSize(img->width,img->height),img->depth,3);
	for(j=0; j<tmp->height;j++)
	{
		for(i=0;i<tmp->width;i++)
		{
			//b=0;
			//r=0;
			//v=0;
			c[0]=0;
			c[1]=0;
			c[2]=0;
			for(y=j-l;y<=j+l;y++)
			{
				m=y-j;
				coef=(1/(sqrt(2*3.14159265)*sigma))*exp(-((m*m)/(2*sigma*sigma)));
					if(y<0 || y>=tmp->height)
					{
						for(k=0;k<img->nChannels;k++)
						{
							c[k]+=getPixel(img,i,j)[k]*coef;
						}
						for(int k2=k;k2<3;k2++)
						{
							c[k2]=c[0];
						}
						//r+=getPixel(img,i,j)[0]*coef;
						//v+=getPixel(img,i,j)[1]*coef;
						//b+=getPixel(img,i,j)[2]*coef;
					}
					else
					{
						for(k=0;k<img->nChannels;k++)
						{
							c[k]+=getPixel(img,i,y)[k]*coef;
						}
						for(int k2=k;k2<3;k2++)
						{
							c[k2]=c[0];
						}
						//r+=getPixel(img,i,y)[0]*coef;
						//v+=getPixel(img,i,y)[1]*coef;
						//b+=getPixel(img,i,y)[2]*coef;
						
					}
			}
			//getPixel(tmp,i,j)[0]=r;
			//getPixel(tmp,i,j)[1]=v;
			//getPixel(tmp,i,j)[2]=b;
			getPixel(tmp,i,j)[0]=c[0];
			getPixel(tmp,i,j)[1]=c[1];
			getPixel(tmp,i,j)[2]=c[2];
		}
	}
	for(j=0; j<tmp->height;j++)
	{
		for(i=0;i<tmp->width;i++)
		{
			//b=0;
			//r=0;
			//v=0;
			c[0]=0;
			c[1]=0;
			c[2]=0;
			for(x=i-l;x<=i+l;x++)
			{
				m=x-i;
				coef=(1/(sqrt(2*3.14159265)*sigma))*exp(-((m*m)/(2*sigma*sigma)));
					if(x<0 || x>=tmp->width)
					{
						for(k=0;k<img->nChannels;k++)
						{
							c[k]+=getPixel(tmp,i,j)[k]*coef;
						}
						for(int k2=k;k2<3;k2++)
						{
							c[k2]=c[0];
						}
						//r+=getPixel(tmp,i,j)[0]*coef;
						//v+=getPixel(tmp,i,j)[1]*coef;
						//b+=getPixel(tmp,i,j)[2]*coef;
					}
					else
					{
						for(k=0;k<img->nChannels;k++)
						{
							c[k]+=getPixel(tmp,x,j)[k]*coef;
						}
						for(int k2=k;k2<3;k2++)
						{
							c[k2]=c[0];
						}
						//r+=getPixel(tmp,x,j)[0]*coef;
						//v+=getPixel(tmp,x,j)[1]*coef;
						//b+=getPixel(tmp,x,j)[2]*coef;
						
					}
			}
			//getPixel(res,i,j)[0]=r;
			//getPixel(res,i,j)[1]=v;
			//getPixel(res,i,j)[2]=b;
			getPixel(res,i,j)[0]=c[0];
			getPixel(res,i,j)[1]=c[1];
			getPixel(res,i,j)[2]=c[2];
			
		}
	}
	return res;
}
